package com.chandra.pos.web.context;


import com.chandra.pos.config.web.WebMvcConfiguration;
import org.springframework.core.annotation.Order;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;

/**
 * Created by Admin on 6/25/14.
 * http://docs.spring.io/spring/docs/3.2.0.BUILD-SNAPSHOT/api/org/springframework/web/WebApplicationInitializer.html
 */

@Order(1)
public class POSWebApplicationInitializer implements WebApplicationInitializer {

//    @Override
//    protected Class<?>[] getRootConfigClasses() {
//        return new Class[] { RootConfiguration.class };
//    }
//
//    @Override
//    protected Class<?>[] getServletConfigClasses() {
//        return new Class<?>[]{ WebMvcConfiguration.class };
//    }
//
//    @Override
//    protected String[] getServletMappings() {
//        return new String[]{"/"};
//    }
//
//
////    @Override
////    protected WebApplicationContext createServletApplicationContext(){
////
////
////    }

    @Override

    public void onStartup(ServletContext container) {

        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.scan("com.chandra.pos");
        // rootContext.register(RootConfiguration.class);

        ContextLoaderListener contextLoaderListener = new ContextLoaderListener(rootContext);

        container.addListener(contextLoaderListener);

        container.setInitParameter("contextInitializerClasses", "com.chandra.pos.web.context.POSApplicationContextInitializer");

        AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();

        webContext.register(WebMvcConfiguration.class);

        DispatcherServlet dispatcherServlet = new DispatcherServlet(webContext);

        ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", dispatcherServlet);

        dispatcher.addMapping("/");

    }
}
